#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl
import requests
from bs4 import BeautifulSoup

# 第一步，获取csrf
r1 = requests.get(url="https://github.com/login")
b1 = BeautifulSoup(r1.text,'html.parser')
r1_cookies = r1.cookies.get_dict()
tag = b1.find(name='input',attrs={'name':'authenticity_token'})

token = tag.get('value')

# 第二步，发送post请求携带用户名密码
r2 = requests.post(
    url='https://github.com/session',
    data={
        'commit':'sing in',
        'utf8':'✓',
        'authenticity_token':token,
        'login':'zcl19940531@163.com',
        'password':'222000aaa'
    },
    cookies=r1_cookies,
)

# 1.获取登录是否成功
    # r2 获取状态码
    # r2 获取响应头【location】
# 2.根据错误提示


#  获取登录成功获得的cookies
r2_cookies = r2.cookies.get_dict()
r1_cookies.update(r2_cookies)
#  第三步。访问个人网页，携带cookies
r3 = requests.get(
    url="https://github.com/settings/profile",
    cookies=r1_cookies
)
print(r3.text)
